package com.ifp.opengate.boot.bean.config;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.ifp.core.cache.CacheManager;
import com.ifp.core.log.Trace;
import com.ifp.opengate.boot.constants.Constants;

/**
 * Created by mogs on 2018/9/1.
 */
public class ConfigBean {

    protected boolean switchSystem = false;

    protected boolean switchApi = false;

    protected boolean switchMerchant = false;

    protected boolean switchMerchantApi = false;

    protected String type;

    private CacheManager cacheManager;

    protected String key;

    private long lastUpdateTime = System.currentTimeMillis();

    /**
     * 是否打印并发信息
     */
    private boolean printAccessInfoFlag = true;

    private long max = -1;

    private long interval = 1000;
    /** 子配置 */
    private Map<String, ConfigBean> subConfigBeanMap = new ConcurrentHashMap<String, ConfigBean>();

    public ConfigBean(boolean switchSystem, boolean switchApi, boolean switchMerchant, boolean switchMerchantApi) {
        this.switchSystem = switchSystem;
        this.switchApi = switchApi;
        this.switchMerchant = switchMerchant;
        this.switchMerchantApi = switchMerchantApi;
    }

    public ConfigBean(boolean switchSystem, boolean switchApi, boolean switchMerchant, boolean switchMerchantApi,
        long max, long interval) {
        this.max = max;
        this.interval = interval;
    }

    public ConfigBean() {

    }

    public void updateCache() {
        try {
            if ((System.currentTimeMillis() - this.lastUpdateTime) > 1000) {
                Map<String, String> transInfo =
                    (Map<String, String>)this.getCacheManager().get(Constants.OPEN_GATE_CACHE_NAME,
                        key.substring(key.indexOf("_") + 1), Constants.CACHE_VALUE_TYPE_MAP);
                long max = Long.parseLong(transInfo.get(this.getType()).split(Constants.SPLIT_STR)[0]);
                long interval = Long.parseLong(transInfo.get(this.getType()).split(Constants.SPLIT_STR)[1]);
                this.setMax(max);
                this.setInterval(interval);
                this.lastUpdateTime = System.currentTimeMillis();
            }
        } catch (Exception e) {
            Trace.log(Trace.MODULE_ACCESS, Trace.LEVEL_ERROR, "update access cache data error !", e);
        }
    }

    public boolean isSwitchSystem() {
        return switchSystem;
    }

    public void setSwitchSystem(boolean switchSystem) {
        this.switchSystem = switchSystem;
    }

    public boolean isSwitchApi() {
        return switchApi;
    }

    public void setSwitchApi(boolean switchApi) {
        this.switchApi = switchApi;
    }

    public boolean isSwitchMerchant() {
        return switchMerchant;
    }

    public void setSwitchMerchant(boolean switchMerchant) {
        this.switchMerchant = switchMerchant;
    }

    public boolean isSwitchMerchantApi() {
        return switchMerchantApi;
    }

    public void setSwitchMerchantApi(boolean switchMerchantApi) {
        this.switchMerchantApi = switchMerchantApi;
    }

    public long getMax() {
        return max;
    }

    public void setMax(long max) {
        this.max = max;
    }

    public long getInterval() {
        return interval;
    }

    public void setInterval(long interval) {
        this.interval = interval;
    }

    public boolean isPrintAccessInfoFlag() {
        return printAccessInfoFlag;
    }

    public void setPrintAccessInfoFlag(boolean printAccessInfoFlag) {
        this.printAccessInfoFlag = printAccessInfoFlag;
    }

    public CacheManager getCacheManager() {
        return cacheManager;
    }

    public void setCacheManager(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    /**
     * 获取子配置
     * 
     * @param key
     * @return
     */
    public ConfigBean getSubConfigBean(String key) {
        return subConfigBeanMap.get(key);
    }

    /**
     * 插入子配置
     * 
     * @param key
     * @return
     */
    public ConfigBean putSubConfigBean(String key, ConfigBean configBean) {
        return subConfigBeanMap.put(key, configBean);
    }

    public Map<String, ConfigBean> getSubConfigBeanMap() {
        return subConfigBeanMap;
    }

    public void setSubConfigBeanMap(Map<String, ConfigBean> subConfigBeanMap) {
        this.subConfigBeanMap = subConfigBeanMap;
    }

}
